package com.xing.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import lombok.Getter;
import lombok.Setter;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.stereotype.Controller;

import com.xing.model.EmployeeInfo;
import com.xing.model.PaymentDetail;
import com.xing.service.EmployeeService;
import com.xing.service.PaymentDetailService;
import com.xing.util.QueryBuilder;
import com.xing.util.Util;

@Controller
public class PaymentDetailAction extends BaseAction {
	/** */
	/**
     * 
     */
	private static final long serialVersionUID = 1L;

	@Resource
	private PaymentDetailService paymentDetailService;

	public void setPaymentDetailService(PaymentDetailService paymentDetailService) {
		this.paymentDetailService = paymentDetailService;
	}
	
	@Resource
	private EmployeeService employeeService;

	@JSON(serialize=false)
	public EmployeeService getEmployeeService() {
		return employeeService;
	}
	
	
	@JSON(serialize=false)
	public String getPaymentDetailJson() throws Exception {
		
		jsonInfo = paymentDetailService.getPaymentDetail(Util.strToInt(intId));
		
		return "getObject";
	}
	
	@JSON(serialize=false)
	public String getContractPaymentJson() throws Exception {
		
		jsonCpList = paymentDetailService.getContractPaymentList(query_contractId);
		
		return "getObject";
	}

	public String list() throws Exception {

		List<EmployeeInfo> employeeInfoList = employeeService.getList();
		
		getRequest().put("employeeInfoList", employeeInfoList);
		
		return "list";
		
	}
	
	@Override
	public String dataGrid() {
		
		Map<String, Object> map = paymentDetailService.getListAndCount(buildQueryCondition(), getPage(), getRows());
		setDataGrid(map);
		
		return "dataGrid";
	} 
	
	public String addPaymentDetail() throws Exception {
		
		PaymentDetail info = createPaymentDetail();
		paymentDetailService.addPaymentDetail(info);
		
		setMessage("msg", "����ɹ���");
		
		return list();
	}
	
	public String modifyPaymentDetail() throws Exception {
		
		PaymentDetail info = createPaymentDetail();
		paymentDetailService.updatePaymentDetail(info);
		
		setMessage("msg", "���³ɹ���");
		
		return list();
	}
	
	public String deletePaymentDetail() throws Exception {
		
		paymentDetailService.deletePaymentDetail(getDeleteId());
		
		setMessage("msg", "ɾ��ɹ���");
		
		return list();
	}

	private PaymentDetail createPaymentDetail() {
		
		PaymentDetail info = new PaymentDetail();

		info.setContractPaymentId(contractPaymentId);
		info.setHandleUserId(handleUserId);
		info.setInvoice(invoice);
		info.setInvoiceAmount(invoiceAmount);
		info.setPaymentAmount(paymentAmount);
		info.setPaymentTime(paymentTime);
		info.setRequestTime(requestTime);
		info.setRemark(remark);
		
		if (Util.strToInt(intId) > 0) {
			info.setIntId(Util.strToInt(intId));
		} 
		
		return info;
	}
	

	private List<String> buildQueryCondition() {
		List<String> conditionList = new ArrayList<String>();
		
		QueryBuilder.buildQueryCondition(query_contractId, "c.contractId", QueryBuilder.LIKE, conditionList);
		QueryBuilder.buildQueryCondition(query_contractName, "c.contractName", QueryBuilder.LIKE, conditionList);
		QueryBuilder.buildQueryCondition(query_contractCode, "c.contractCode", QueryBuilder.LIKE, conditionList);
		QueryBuilder.buildTimeQueryCondition(query_startPaymentTime, query_endPaymentTime, "pd.paymentTime", conditionList);
		
		return conditionList;
	}
	
	
	private Map<String, Object> jsonInfo;
	@JSON(name="paymentDetailJson")
	public Map<String, Object> getJsonInfo() {
		return jsonInfo;
	}


	private List<Map<String, Object>> jsonCpList;
	@JSON(name="contractPaymentJson")
	public List<Map<String, Object>> getJsonCpList() {
		return jsonCpList;
	}
	
	
	@Getter @Setter private String intId;
	@Getter @Setter private int contractPaymentId;
	@Getter @Setter private double paymentAmount;
	@Getter @Setter private int invoice;
	@Getter @Setter private double invoiceAmount;
	@Getter @Setter private Timestamp paymentTime;
	@Getter @Setter private Timestamp requestTime;
	@Getter @Setter private int handleUserId;
	@Getter @Setter private String remark;	
	
	
	@Getter @Setter private String query_contractId;
	@Getter @Setter private String query_contractName;
	@Getter @Setter private String query_contractCode;
	@Getter @Setter private String query_startPaymentTime;
	@Getter @Setter private String query_endPaymentTime;
	
}
